import { mapActions, mapGetters } from 'vuex';
export default {
  computed: {
    ...mapGetters(['cachedRoutes'])
  },
  methods: {
    ...mapActions(['setCachedRoutes']),
    /**
    * 设置路由缓存相关配置
    * @param: queryKey 页面搜索表单对象的key
    */
    setSearchQuery(queryKey, defaultName = '') {
      const name = defaultName || this.$route.name;
      if (typeof this.setCachedRoutes === 'function') {
        this.$store.dispatch('routerCache/setCachedRoutes', { name: name, query: JSON.stringify(this[queryKey]) });
      }
    },
    /**
     * 获取路由缓存相关配置
     * @param: queryKey 页面搜索表单对象的key
     */
    getSearchQuery(queryKey, defaultName = '') {
      // 如果使用 路由 的 name属性缓存的话，这里就取当前页面路由的 name值，如果使用了 path 缓存替换即可
      const name = defaultName || this.$route.name;
      const cachedRoutes = this.cachedRoutes || {};
      if (Object.prototype.hasOwnProperty.call(cachedRoutes, name)) {
        const target = JSON.parse(cachedRoutes[name]);
        this[queryKey] = Object.assign({}, target);
      }
    }
  }
};
